<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>C++ Tutorial: References for Built-in Types</title>
  <meta
 content="C++ Tutorial: References for Built-in Types"
 name="description" />
  <meta
 content="C++ Tutorial, References for Built-in Types, Differences between pointer and reference"
 name="keywords" />
  <meta http-equiv="Content-Type"
 content="text/html; charset=ISO-8859-1" />
  <link type="text/css" rel="stylesheet" href="../images/bogostyleWidePre.css" />
</head>
<body>
<div id="page" align="center">
<div id="content" style="width: 800px;">
<div id="logo">
<div class="whitetitle" style="margin-top: 70px;">bogotobogo </div>
</div>
<div id="topheader">
<div class="headerbodytext" align="left"><br />
<strong>Bogotobogo</strong><br />
contact@bogotobogo.com </div>
<div class="smallgraytext" id="toplinks"><a href="../index.html">Home</a>
| <a href="../sitemap.html">Sitemap</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="menu">
<div class="smallwhitetext" style="padding: 9px;" align="right"><a
 href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">Products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="../blog" target="_blank">Contact Us</a>
</div>
</div>
<div id="submenu">
<div class="smallgraytext" style="padding: 9px;" align="right">
<a href="../gif.html">Gif</a> 
|<a href="../java_applet.html">JavaApplet/Web Start</a>
| <a href="../flash.html">Flash</a>
| <a href="../shockwave.html">ShockWave</a>
| <a href="../svg.html">SVG</a>
| <a href="../iPhone.html">iPhone/iPad</a>
| <a href="../android.html">Android</a>
| <a href="../OnHTML5.html">HTML5</a>
| <a href="../Algorithms/algorithms.html">Algorithms</a>
| <a href="../News/NewsMain.html">News</a>
| <a href="../cplusplus/cpptut.html">C++</a>
| <a href="../Java/tutorial/on_java.html">Java</a>
| <a href="../php/phptut.html">PHP</a>
| <a href="../DesignPatterns/introduction.html">Design Patterns</a>
| <a href="../python/pytut.html">Python</a> 
| <a href="../CSharp/.netframework.html">C#</a>
| <a href="../forums.html">Forums</a> 
| <a href="../VisualBasicSQL/introduction.html">Visual Basic</a>
</div>
</div>

<div id="contenttext">
<br />
<!-- Use of this code assumes agreement with the Google Custom Search Terms of Service. -->
<!-- The terms of service are available at http://www.google.com/cse/docs/tos.html -->
<form name="cse" id="searchbox_demo" action="http://www.google.com/cse">
  <input type="hidden" name="cref" value="" />
  <input type="hidden" name="ie" value="utf-8" />
  <input type="hidden" name="hl" value="" />
  <input name="q" type="text" size="40" />
  <input type="submit" name="sa" value="Search" />
</form>
<script type="text/javascript" src="http://www.google.com/cse/tools/onthefly?form=searchbox_demo&lang="></script>


<br />
<div style="padding: 10px;"><span class="titletext">C++ Tutorial -   References for Built-in Types</span></div>

<div id="bookmarkshare">
<script type="text/javascript">var addthis_config = {"data_track_clickback":true};</script>
<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&amp;username=khhong7"><img src="http://s7.addthis.com/static/btn/v2/lg-share-en.gif" width="125" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=khhong7"></script>
</div>


<img src="../images/cplusplus/cpp_logo.jpg" alt="cplusplus logo"/>

<div class="bodytext" style="padding: 12px;" align="justify"> 
<div class="subtitle_2nd" id="FullList">Full List of C++ Tutorials</div>
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
   <li><a href="dynamic_cast.html">Dynamic Cast Operator</a></li>
   <li><a href="typecast.html">Type Cast Operators</a></li>
   <li><a href="autoptr.html">Class auto_ptr</a></li>
   <li><a href="templates.html">Templates</a></li>
   <li><a href="references.html">References for Built-in Types</a></li>
   <li><a href="valuevsreference.html">Pass by Value vs. Pass by Reference</a></li>
   <li><a href="memoryallocation.html">Memory Allocation</a></li>
   <li><a href="friendclass.html">Friend Functions and Friend Classes</a></li>
   <li><a href="functors.html">Functors (Function Objects)</a></li>
   <li><a href="statics.html">Static Variables and Static Class Members</a></li>
   <li><a href="exceptions.html">Exceptions</a></li>
   <li><a href="pointers.html">Pointers</a></li>
   <li><a href="pointers2.html">Pointers II</a></li>
   <li><a href="pointers3.html">Pointers III</a></li>
   <li><a href="assembly.html">Taste of Assembly</a></li>
   <li><a href="smallprograms.html">Small Programs</a></li>
   <li><a href="linkedlist.html">Linked List Examples</a></li>
   <li><a href="binarytree.html">Binary Tree Example Code</a></li>
   <li><a href="stl.html">Standard Template Library (STL)</a></li>
   <li><a href="stl2.html">Standard Template Library (STL) II - Maps</a></li>
   <li><a href="stl3_iterators.html">Standard Template Library (STL) III - Iterators</a></li>
   <li><a href="slicing.html">Object Slicing and Virtual Table</a></li>
   <li><a href="this_pointer.html">The this Pointer</a></li>
   <li><a href="stackunwinding.html">Stack Unwinding</a></li>
   <li><a href="upcasting_downcasting.html">Upcasting and Downcasting</a></li>
   <li><a href="object_returning.html">Object Returning</a></li>
   <li><a href="private_inheritance.html">Private Inheritance</a></li>
   <li><a href="cplusplus_keywords.html">C++_Keywords</a></li>
   <li><a href="multithreaded.html">Multi-Threaded Programming - Terminology</a></li>
   <li><a href="multithreaded2A.html">Multi-Threaded Programming II - Native Thread for Win32 (A) </a></li>
   <li><a href="multithreaded2B.html">Multi-Threaded Programming II -  Native Thread for Win32 (B) </a></li>
   <li><a href="multithreaded2C.html">Multi-Threaded Programming II -  Native Thread for Win32 (C) </a></li>
   <li><a href="multithreaded2.html">Multi-Threaded Programming II - C++ Thread for Win32</a></li>
   <li><a href="multithreaded3.html">Multi-Threaded Programming III - C++ Class Thread for Pthreads</a></li>
   <li><a href="multithreadedDebugging.html">Multithread Debugging</a></li>
   <li><a href="embeddedSystemsProgramming.html">Embedded Systems Programming</a></li>
   <li><a href="boost.html">Boost</a></li>
   <li>Programming Questions and Solutions
    <ul>
       <li><a href="quiz_strings_arrays.html">Strings and Arrays</a></li>
       <li><a href="quiz_linkedlist.html">Linked List</a></li>
       <li><a href="quiz_recursion.html">Recursion</a></li>
       <li><a href="quiz_bit_manipulation.html">Bit Manipulation</a> </li>
       <li><a href="google_interview_questions.html">140 Google Interview Questions</a> </li>
    </ul>

   </li>
</ul>
</div>
<br />
<br />
<img src="images/reference/ImsilOkJungHo.png" alt="ImsilOkJungHo"/>
<br />
<br />
<div class="bodytext" style="padding: 12px;" align="justify">
<div class="subtitle" id="whyreferences">Why do we need them when you have pointers?</div>
<p>A reference variable is actually just a pointer that reduces syntactical clumsiness related with pointers in C (reference variables are internally implemented as a pointer; itï¿½s just that programmers can't use it the way they use pointers).</p>
<p>
As a side note, a reference must refer to some object at all times, but a pointer can point to NULL. In this way, references can be more efficient when you know that you'll always have an object to point to, because you don't have to check against NULL</p>
<br />
<br />



<div class="subtitle" id="referencesforbuiltintypes">References for Built-in Types</div>
<p>A reference is an alias for an object. It allows us to manipulate an object in a similar way of using pointer without pointer syntax.<br />
In practice, references are primarily used as parameters to a function such as an object passed into a function.</p>
<p>But in this chapter we'll only deal with references for built-in types.</p>
<p>
A reference <strong>must be initialized</strong>. Once defined, a reference cannot be made to refer to another object and this is why it must be initialized.</p>
<pre>
int ival = 128;
int &rval = ival;	// rval is a reference to ival
int &rval2;		// Error: a reference must be initialized  	
</pre>
<p>By changing the value of reference, we can change the value of the <strong>referenced</strong> original variable.</p>
<pre>
rval++;		// ival is now 129.
</pre>
<p>Though a reference is a kind of pointer, it is not correct to initialize it to the address of an object, as we would do a pointer.</p>
<pre>
int ival = 128;
int &rval = &ival;	// Error: rval is of type int, not int*
</pre>
<p>How about assigning a <strong>non-addressable</strong> value (such as literal constant) to the reference?</p>
<pre>
int &rval2 = 256;
</pre> 
<p>Then, we'll get the error message:</p>
<pre>
error C2440: 'initializing' : cannot convert from 'int' to 'int &'
</pre>
<p>A reference always refers to the variable with which it was initialized. We can't resign a reference to refer to another variable. So, for example, the result of the code below might not be obvious:</p>
<pre>
int herWeight = 110;
int& <font color="blue">WeightOfHermione</font> = herWeight;
int WeightOfHarry = 170;
WeightOfHermione = WeightOfHarry;
WeightOfHarry = 200;
</pre>
<p>The line 
<pre>
WeightOfHermione = WeightOfHarry;
</pre>
does not reassign the reference <strong>WeightOfHermione</strong> so it refers to <strong>WeightOfHarry</strong> because a reference can't be reassigned. However, because <strong>WeightOfHermione</strong> is just another name for <strong>herWeight</strong>, the code 
<pre>
WeightOfHermione = WeightOfHarry;
</pre>
is equivalent to 
<pre>
herWeight = WeightOfHarry;
</pre> which assigns <strong>170</strong> to <strong>herWeight</strong>. The final outcome: <strong>herWeight</strong> becomes <strong>170</strong> and <strong>WeightOfHermione</strong> still refers to <strong>herWeight</strong> and it is <strong>170</strong> not <strong>200</strong>. </p>
<p>And How about assigning an object of a <strong>different type</strong> (assuming there is a conversion from the one type to the other) to the reference?</p>
<pre>
double dval = 3.14;
int &rval3 = dval;
</pre>
<p>Again, we'll get similar error:</p>
<pre>
error C2440: 'initializing' : cannot convert from 'double' to 'int &'
</pre>
<p>But things are different if it's a <strong>const</strong> reference.</p>
<pre>
const &rval4 = 1024;
dval = 3.14;
const &rval5 = dval;
</pre>
<p>The conversion is done by the compiler using temporary object.</p>
<p>Two primary differences between a reference and pointer are:</p>
<ol>
	<li>a reference must always refer to an object</li>
<p>For pointer example:
<pre>
int *ptr = 0;
</pre>
Here, the pointer, ptr, currently addresses no object, but for reference,
<pre>
const int &r = 0;
</pre>
internally, following takes place and the reference, r, still refers an object.
<pre>
int temp = 0;
const int &r = temp;
</pre>

	<li>the assignment of one reference with another changes the object referenced and the reference itself</li>
<p>Following code is an example for pointers:
<pre>
int i1 = 1, i2 = 2;
int *ptr1 = &i1, *ptr2 = &i2;
p1 = p2;
</pre>
Here, p1 and p2 now both address the same object whose value is 2. This can be a significant source of program error.<br />
But for references,
<pre>
#include &lt;iostream&gt;
using namespace std;
int main() 
{
	int i1 = 1000, i2 = 2000;

	int &r1 = i1;
	int &r2 = i2;

	cout << "r1=" << r1 << " r2=" << r2 << endl;
	cout << "&r1=" << &r1 << " &r2=" << &r2 << endl;
	r1 = r2;
	cout << "r1=" << r1 << " r2=" << r2 << endl;
	cout << "&r1=" << &r1 << " &r2=" << &r2 << endl;
}
</pre>
with output
<pre>
r1=1000 r2=2000
&r1=0017FF28 &r2=0017FF1C
r1=2000 r2=2000
&r1=0017FF28 &r2=0017FF1C
</pre>
It is <strong>i1</strong>, the value referenced by r1, that is changed, and not the reference. After the assignment, the two references still refer to their original objects.</p>
</ol>
<br />
<p>For references with a structure and classes, please try other chapters of this tutorial.</p>

<br />
<br />
<img src="images/reference/ImsilOkJungHo.png" alt="ImsilOkJungHo"/>
<br />
<br />

<br /><br />
<div class="subtitle_2nd" id="FullList">List of C++ Tutorials</div>
<br />
<ul>
   <li><a href="cpptut.html">C++ Home</a> </li>
   <li><a href="string.html">String</a> </li>
   <li><a href="constructor.html">Constructor</a> </li>
   <li><a href="operatoroverloading.html">Operator Overloading</a> </li>
   <li><a href="virtualfunctions.html">Virtual Functions</a></li>
   <li><a href="dynamic_cast.html">Dynamic Cast Operator</a></li>
   <li><a href="typecast.html">Type Cast Operators</a></li>
   <li><a href="autoptr.html">Class auto_ptr</a></li>
   <li><a href="templates.html">Templates</a></li>
   <li><a href="references.html">References for Built-in Types</a></li>
   <li><a href="valuevsreference.html">Pass by Value vs. Pass by Reference</a></li>
   <li><a href="memoryallocation.html">Memory Allocation</a></li>
   <li><a href="friendclass.html">Friend Functions and Friend Classes</a></li>
   <li><a href="functors.html">Functors (Function Objects)</a></li>
   <li><a href="statics.html">Static Variables and Static Class Members</a></li>
   <li><a href="exceptions.html">Exceptions</a></li>
   <li><a href="pointers.html">Pointers</a></li>
   <li><a href="pointers2.html">Pointers II</a></li>
   <li><a href="pointers3.html">Pointers III</a></li>
   <li><a href="assembly.html">Taste of Assembly</a></li>
   <li><a href="smallprograms.html">Small Programs</a></li>
   <li><a href="linkedlist.html">Linked List Examples</a></li>
   <li><a href="binarytree.html">Binary Tree Example Code</a></li>
   <li><a href="stl.html">Standard Template Library (STL)</a></li>
   <li><a href="stl2.html">Standard Template Library (STL) II - Maps</a></li>
   <li><a href="stl3_iterators.html">Standard Template Library (STL) III - Iterators</a></li>
   <li><a href="slicing.html">Object Slicing and Virtual Table</a></li>
   <li><a href="this_pointer.html">The this Pointer</a></li>
   <li><a href="stackunwinding.html">Stack Unwinding</a></li>
   <li><a href="upcasting_downcasting.html">Upcasting and Downcasting</a></li>
   <li><a href="object_returning.html">Object Returning</a></li>
   <li><a href="private_inheritance.html">Private Inheritance</a></li>
   <li><a href="cplusplus_keywords.html">C++_Keywords</a></li>
   <li><a href="multithreaded.html">Multi-Threaded Programming - Terminology</a></li>
   <li><a href="multithreaded2A.html">Multi-Threaded Programming II - Native Thread for Win32 (A) </a></li>
   <li><a href="multithreaded2B.html">Multi-Threaded Programming II -  Native Thread for Win32 (B) </a></li>
   <li><a href="multithreaded2C.html">Multi-Threaded Programming II -  Native Thread for Win32 (C) </a></li>
   <li><a href="multithreaded2.html">Multi-Threaded Programming II - C++ Thread for Win32</a></li>
   <li><a href="multithreaded3.html">Multi-Threaded Programming III - C++ Class Thread for Pthreads</a></li>
   <li><a href="multithreadedDebugging.html">Multithread Debugging</a></li>
   <li><a href="embeddedSystemsProgramming.html">Embedded Systems Programming</a></li>
   <li><a href="boost.html">Boost</a></li>
   <li>Programming Questions and Solutions
    <ul>
       <li><a href="quiz_strings_arrays.html">Strings and Arrays</a></li>
       <li><a href="quiz_linkedlist.html">Linked List</a></li>
       <li><a href="quiz_recursion.html">Recursion</a></li>
       <li><a href="quiz_bit_manipulation.html">Bit Manipulation</a> </li>
       <li><a href="google_interview_questions.html">140 Google Interview Questions</a> </li>
    </ul>
   </li>
</ul>
<br /><br />


<br />


<br />
<br />
<br />


</div>
</div>
<div class="smallgraytext" id="footer"><a href="../index.html">Home</a>
| <a href="../about_us.html">About Us</a>
| <a href="../products.html">products</a>
| <a href="../our_services.html">Our Services</a>
| <a href="#">Contact Us</a>
| Bogotobogo &copy; 2011 | <a target="_blank" href="http://www.bogotobogo.com">Bogotobogo </a>
</div>
</div>
</div>
</body>
</html>
